/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ar.com.wholeconsulting.pacc.gui.utils.treetable.generic;

import ar.com.wholeconsulting.pacc.gui.utils.treetable.PACCAbstractTreeTableModel;
import ar.com.wholeconsulting.pacc.gui.utils.treetable.TreeTableNode;
import java.util.List;
import org.jdesktop.swingx.JXTreeTable;

/**
 *
 * @author Duco Pablo
 */
public class GenericTreeTableModel extends PACCAbstractTreeTableModel {
    
    private JXTreeTable tree;
    private EachElementTableEditor treeEditor;
    
    public GenericTreeTableModel(JXTreeTable tree) {
        super(new String[]{"Atributo", "Valor"});
        this.tree = tree;
        this.tree.setTreeTableModel(this);
        treeEditor = new EachElementTableEditor(this, tree);
        tree.getColumn(1).setCellEditor(treeEditor);
        setIcons();
    }
    
    public void initTreeTable(List<GenericEditorItems> list) {
        for (GenericEditorItems item : list) {
            this.addToRoot(GenericTreeTableNode.getNodeStruncture(item, treeEditor.getEditors()));
        }
    }
    
    @Override
    public Object getValueAt(Object node, int column) {
        //System.out.println("getValueAt: " + node + ", " + column);
        Object value=null;
        TreeTableNode treenode = (TreeTableNode) node;
        switch (column) {
            case 0:
                 value=treenode.getName();
                break;
            case 1:
                value=treenode.getDescription();
                break;
            default:
                return "Unknown";
        }
        if (value==null) {
            value="";
        }
        return value;
    }
    
    @Override
    public boolean isCellEditable(Object node, int column) {
        TreeTableNode treenode = (TreeTableNode) node;
        switch (column) {
            default:
                return false;
            case 1:
                if (isLeaf(treenode)) {
                    return true;
                } else {
                    return false;
                }
        }
    }
    
    @Override
    public void setValueAt(Object value, Object node, int column) { 
        if (node instanceof GenericTreeTableNode) {
            GenericTreeTableNode generic = (GenericTreeTableNode) node;
            generic.setValue(value);
        }
    }

    private void setIcons() {
        tree.setLeafIcon(new javax.swing.ImageIcon(getClass().getResource("/16x16/page_white_edit.png")));
        tree.setClosedIcon(new javax.swing.ImageIcon(getClass().getResource("/16x16/box_closed.png")));
        tree.setOpenIcon(new javax.swing.ImageIcon(getClass().getResource("/16x16/box_open.png")));
    }
}
